package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldValueMapper;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldValue;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateUser;
import com.ovopark.organize.server.model.vo.FranchiseesApprovalTemplateFieldValueVo;
import com.ovopark.organize.server.model.vo.FranchiseesApprovalTemplateValueVo;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldValueService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateUserService;
import com.ovopark.organize.server.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wxb
 * @Title: FranchiseesApprovelServiceImpl
 * @Description: 加盟商立项模板字段选择项数据
 * @date 2024/4/11 15:51
 **/
@Service
@Slf4j
public class FranchiseesApprovalTemplateFieldValueServiceImpl extends ServiceImpl<FranchiseesApprovalTemplateFieldValueMapper, FranchiseesApprovalTemplateFieldValue> implements FranchiseesApprovalTemplateFieldValueService {

    @Resource
    private FranchiseesApprovalTemplateUserService franchiseesApprovalTemplateUserService;

    @Override
    public Map<Integer, FranchiseesApprovalTemplateFieldValue> getFieldValueList(Integer sectionId, Integer templateId, Integer groupId, Integer approvalId, String version, List<Integer> fieldIdList) {
        LambdaQueryWrapper<FranchiseesApprovalTemplateFieldValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getGroupId, groupId);
        queryWrapper.in(FranchiseesApprovalTemplateFieldValue::getFieldId, fieldIdList);
        queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getIsDeleted, 0);
        List<FranchiseesApprovalTemplateFieldValue> list = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            Map<Integer, FranchiseesApprovalTemplateFieldValue> map = list.stream().collect(Collectors.toMap(FranchiseesApprovalTemplateFieldValue::getFieldId, Function.identity(), (k1, k2) -> k1));
            return map;
        }
        return null;
    }

    /**
     * @description: 保存模板字段值
     * @param: [vo, userId]
     * @return: void
     * @author wxb
     * @date: 2024/6/5 9:53
     */
    @Override
    public void saveValue(FranchiseesApprovalTemplateValueVo vo, Integer userId) {
        Integer groupId = vo.getGroupId();
        // 地址id
        Integer addressId = vo.getAddressId();
        // 模板id
        Integer templateId = vo.getTemplateId();
        // 模版名称
        String templateName = vo.getTemplateName();
        // 模板配置的版本号
        String version =  vo.getVersion();
        // 数据ID
        Integer templateUserId =  vo.getTemplateUserId();
        // 得分
        Integer score = vo.getScore();
        List<FranchiseesApprovalTemplateFieldValueVo> valueList = vo.getValueList();
        FranchiseesApprovalTemplateUser templateUser = null;
        if (templateUserId != null) {
            // 查询是否已经存在
            templateUser = franchiseesApprovalTemplateUserService.getById(templateUserId);
        }
        if (templateUser != null) {
            templateUser.setScore(score);
            templateUser.setUpdateId(userId);
            templateUser.setUpdateTime(LocalDateTime.now());
            // 删除value
            this.deleteByTemplateUserId(templateUserId);
        } else if (templateId != null && addressId != null && version != null) {
            // 新增
            templateUser = new FranchiseesApprovalTemplateUser();
            templateUser.setGroupId(groupId);
            templateUser.setAddressId(addressId);
            templateUser.setTemplateId(templateId);
            templateUser.setTemplateName(templateName);
            templateUser.setVersion(version);
            templateUser.setUserId(userId);
            templateUser.setCreateTime(LocalDateTime.now());
            templateUser.setScore(score);
            // 直接保存
            franchiseesApprovalTemplateUserService.saveOrUpdateById(templateUser);
        }
        if (CollectionUtil.isNotEmpty(valueList)) {
            for (int i = 0; i < valueList.size(); i++) {
                FranchiseesApprovalTemplateFieldValueVo value = valueList.get(i);
                FranchiseesApprovalTemplateFieldValue fieldValue = new FranchiseesApprovalTemplateFieldValue();
                fieldValue.setGroupId(groupId);
                fieldValue.setFieldId(value.getFieldId());
                fieldValue.setFieldType(value.getFieldType());
                fieldValue.setValue(value.getValue());
                fieldValue.setValueName(value.getValueName());
                fieldValue.setTemplateUserId(templateUser.getId());
                fieldValue.setCreateTime(LocalDateTime.now());
                fieldValue.setUserId(userId);
                fieldValue.setSort(i);
                this.baseMapper.insert(fieldValue);
            }
        }
    }

    @Override
    public List<FranchiseesApprovalTemplateFieldValue> getFieldValueListByTemplateUserId(Integer templateUserId, Integer groupId) {
        List<FranchiseesApprovalTemplateFieldValue> list = new ArrayList<>();
        if (templateUserId != null) {
            LambdaQueryWrapper<FranchiseesApprovalTemplateFieldValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getTemplateUserId, templateUserId);
            queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getGroupId, groupId);
            queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getIsDeleted, 0);
            queryWrapper.orderByAsc(FranchiseesApprovalTemplateFieldValue::getSort);
            list = this.baseMapper.selectList(queryWrapper);
        }
        return list;
    }

    /** 
     * @description: 删除数据
     * @param: [templateUserId]
     * @return: void
     * @author wxb
     * @date: 2024/6/6 17:31
     */ 
    @Override
    public void deleteByTemplateUserId(Integer templateUserId) {
        if (templateUserId != null) {
            LambdaQueryWrapper<FranchiseesApprovalTemplateFieldValue> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesApprovalTemplateFieldValue::getTemplateUserId, templateUserId);
            this.baseMapper.delete(queryWrapper);
        }
    }
}
